﻿using FreeScheduler;
using Gengens.Disk.WebApi.util;
using Serilog;

namespace Gengens.Disk.WebApi
{
    public class SchedulerLauncher
    {
        private static Scheduler Scheduler => MateData.Scheduler;

        public static void Init() {
            CleanPreFile();
        }

        /// <summary>
        /// 清理前置文件
        /// </summary>
        public static void CleanPreFile() {
            string id = Scheduler.AddTask("清理前置文件", "不明所以的BODY", round: -1, 10);
            MateData.SchedulerTaskEvent += task => {
                if (task.Id == id) {
                    var now = DateTime.Now;
                    var waitRmList = FileUtils.PreFileCache.FindAll(f => (now - f.UpdateTime).TotalSeconds > 200);
                    if (ListUitls.HasValue(waitRmList)) {
                        waitRmList.ForEach(f =>
                        {
                            try
                            {
                                File.Delete(f.Url);
                            }
                            catch (Exception) { }
                        });
                        FileUtils.PreFileCache.RemoveAll(f=>waitRmList.Contains(f));
                    }
                    Log.Information($"清理了{waitRmList.Count}个前置上传文件");
                }
            };
        }
        
        

    }

    
}
